<template>
	<xn-form-container
		:title="formData.id ? '编辑互助金成员信息记录表' : '增加互助金成员信息记录表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="互助金成员姓名：" name="memberName">
				<a-input v-model:value="formData.memberName" placeholder="请输入互助金成员姓名" allow-clear />
			</a-form-item>
			<a-form-item label="互助金成员年龄：" name="memberAge">
				<a-input v-model:value="formData.memberAge" placeholder="请输入互助金成员年龄" allow-clear />
			</a-form-item>
			<a-form-item label="互助金成员性别：" name="memberGen">
				<a-select v-model:value="formData.memberGen" placeholder="请选择互助金成员性别" :options="memberGenOptions" />
			</a-form-item>
			<a-form-item label="互助金成员身份证号：" name="memberIdcard">
				<a-input v-model:value="formData.memberIdcard" placeholder="请输入互助金成员身份证号" allow-clear />
			</a-form-item>
			<a-form-item label="互助金成员申请信息(通过，拒绝，审核中)：" name="applyStatus">
				<a-input
					v-model:value="formData.applyStatus"
					placeholder="请输入互助金成员申请信息(通过，拒绝，审核中)"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="申请文件：" name="applyFileUrl">
				<a-input v-model:value="formData.applyFileUrl" placeholder="请输入申请文件" allow-clear />
			</a-form-item>
			<a-form-item label="互助金工作情况：" name="memberWork">
				<a-input v-model:value="formData.memberWork" placeholder="请输入互助金工作情况" allow-clear />
			</a-form-item>
			<a-form-item label="入职/退休时间：" name="memberWorkTime">
				<a-date-picker
					v-model:value="formData.memberWorkTime"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择入职/退休时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="附件上传" name="isPay">
				<a-input v-model:value="formData.isPay" placeholder="请输入实际缴纳互助金费用" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizCooperationForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCooperationApi from '@/api/biz/bizCooperationApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const memberGenOptions = ref([])
	const memberOrgOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		memberGenOptions.value = tool.dictList('GENDER')
		memberOrgOptions.value = tool.dictList('CONTENT_TYPE')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizCooperationApi
					.bizCooperationSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
